package com.huidian.resource.server.controller;

import com.huidian.cache.aop.TransObj4DelCache;
import com.huidian.cache.handler.CacheHandler;
import com.huidian.cache.handler.IPublishResourceModify;
import com.huidian.resource.server.entity.Student;
import com.huidian.resource.server.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author tjs
 */
@RestController
@RequestMapping("/resource")
@Slf4j
public class ResourceCrudController {

    @Resource
    private IStudentService studentService;

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private IPublishResourceModify publishResourceModify;

    @PostMapping("/add")
    public void add(@RequestBody Student student){
        studentService.addSelective(student);
    }

    @PostMapping("/batchadd")
    public void batchadd(@RequestBody List<Student> studentList){
        studentService.addBatch(studentList);
    }

    @GetMapping("/query")
    public Object query(@RequestParam Long stdId){
        return studentService.selectById(stdId);
    }

    @GetMapping("/querylist")
    public Object querylist(@RequestBody Student student){
        return studentService.selectList(student);
    }

    @PostMapping("/update")
    public void update(@RequestBody Student student){
        studentService.updateObj(student);
    }

    @PostMapping("/batchupdate")
    public void batchupdate(@RequestBody List<Student> studentList){
        studentService.batchUpdateObj(studentList);
    }


    @DeleteMapping("/delete")
    public void delete(@RequestParam Long stdId){
        studentService.deleteById(stdId);
    }

    @DeleteMapping("/batchdelete")
    public void batchdelete(@RequestParam List<Long> stdIdList){
        studentService.deleteByIdList(stdIdList);
    }

    @GetMapping("/manulremovecache")
    public void manulremovecache(@RequestParam String  cacheManagerName, @RequestParam String  cacheName, @RequestParam List<Object> removeKeyList, @RequestParam Set<String> cacheNameSet4Clear) throws  Exception{
        TransObj4DelCache transObj4DelCache = CacheHandler.createTransObj4DelCache(applicationName,cacheManagerName, Collections.singletonMap(cacheName,removeKeyList),cacheNameSet4Clear,0);
        CacheHandler.handleCacheModify(publishResourceModify,transObj4DelCache);
    }

}
